package com.hd.core.project.module.controller;

import java.util.Arrays;

import java.util.List;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hd.core.common.utils.StringUtils;
import com.hd.core.project.module.base.WrapperBaseController;
import com.hd.core.project.module.config.StudentModuleProperties;
import com.hd.core.project.module.domain.Select2;
import com.hd.core.project.module.domain.Classes;
import com.hd.core.project.module.service.ClassesService;
import com.hd.core.project.module.service.ModuleUserService;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hd.core.framework.aspectj.lang.annotation.Log;
import com.hd.core.framework.aspectj.lang.constant.BusinessType;
import com.hd.core.framework.web.page.TableDataInfo;
import com.hd.core.framework.web.domain.AjaxResult;

/**
 * <p>班级列表-控制层</p>
 * 
 * @since JDK 1.8
 * @version  V1.0
 * @author RichardTang 
 * @date: 2018年8月20日
 * @package com.hd.core.project.module.controller
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Validated
@Controller
@RequestMapping("/module/student/classes")
public class ClassesController extends WrapperBaseController<StudentModuleProperties> {

	@Autowired
	private ClassesService classesService;
	
	@Autowired
	private ModuleUserService moduleUserService;

	ClassesController() {
		super("/classes");
	}

	/**
	 * 跳转到显示视图
	 */
	@GetMapping
	@RequiresPermissions("module-student:classes:view")
	public String view(){
	    return getResponseViewPath("/classes");
	}
	
	/**
	 * <p>查询班级数据</p>
	 * <p>根据条件实体来查询</p>
	 * 
	 * @param entity 条件实体
	 * @return 返回查询到的数据 这里使用的是bootstrap-table的表格来渲染的
	 */
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("module-student:classes:list")
	public TableDataInfo list(Classes entity){
		startPage();
		List<Classes> result = classesService.selectList(new EntityWrapper<Classes>(entity));
		return getDataTable(result);
	}

	/**
	 * <p>跳转到添加班级视图页面</p>
	 * 
	 * @return 跳转视图的路径
	 */
	@GetMapping("/add")
	public String add(){
		return getResponseViewPath("/add");
	}
	
	/**
	 * <p>添加一个班级信息</p>
	 * 
	 * @param entity 数据实体
	 * @return http相应体
	 */
	@ResponseBody
	@PostMapping("/add")
	@Log(title = "班级列表", action = BusinessType.INSERT)
	@RequiresPermissions("module-student:classes:add")
	public AjaxResult addSave(Classes entity){
		return toAjax(classesService.insert(entity));
	}

	/**
	 * <p>根据id查询出需要修改的数据,并回现到修改的页面</p>
	 * @param id 需要查询的id
	 * @param mmap 域对象
	 * @return 修改的视图路径
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap){
		Classes classList = classesService.selectById(id);
		mmap.put("classes", classList);
	    return getResponseViewPath("/edit");
	}
	
	/**
	 * <p>保存需要修改的数据,根据数据实体来修改</p>
	 * @param entity 数据实体
	 * @return http相应体信息
	 */
	@ResponseBody
	@RequiresPermissions("module-student:classes:edit")
	@Log(title = "班级列表", action = BusinessType.UPDATE)
	public AjaxResult editSave(Classes entity){
		return toAjax(classesService.updateById(entity));
	}
	
	/**
	 * <p>根据id删除对应的班级信息</p>
	 * @param ids 班级id
	 * @return http相应体信息
	 */
	@ResponseBody
	@PostMapping( "/remove")
	@RequiresPermissions("module-student:classes:remove")
	@Log(title = "班级列表", action = BusinessType.DELETE)
	public AjaxResult remove(String[] ids){
		List<String> idList = Arrays.asList(ids);
		return toAjax(classesService.deleteClassesById(idList));
	}

	/**
	 * <p>根据id来查询教师的名称</p>
	 * 
	 * @param id 对应的id
	 * @return 教师名称
	 */
	@ResponseBody
	@GetMapping("/queryTeacherName/{id}")
	@RequiresPermissions("module-student:classes:query-tname")
	public String queryTeacherNameById(@PathVariable("id") Long id){
		//这里使用了springboot的simpleCache来做缓存
		// 所以这里的类型必须和User实体的id的类型一致否则使用时会出现问题
		return moduleUserService.selectTeacherById(id);
	}

	/**
	 * <p>检查教师名称是否可选择</p>
	 * 
	 * @param id 教师的id
	 * @param name 教师的名称
	 * @return 1:不可选，0:可选择
	 */
	@ResponseBody
	@PostMapping("/checkMasterTeacher")
	public String checkMasterTeacher(Long id,String name){
		String uniqueFlag = "1";
		if (StringUtils.isNotNull(id) && StringUtils.isNotNull(name)){
			uniqueFlag = moduleUserService.checkMasterTeacher(id,name);
		}
		return uniqueFlag;
	}

	/**
	 * <p>校验班级的名称是否可选择</p>
	 * 
	 * @param className 班级名称
	 * @return 1:不可选，0:可选择
	 */
	@ResponseBody
	@PostMapping("/checkClassName")
	public String checkClassName(String className){
		String uniqueFlag = "1";
		if (StringUtils.isNotNull(className)){
			uniqueFlag = classesService.checkName(className);
		}
		return uniqueFlag;
	}
	
	/**
	 * <p>查询符合做班主任条件的教师 并且以Select2的格式返回</p>
	 * 
	 * @return select2格式的教师数据
	 */
	@ResponseBody
	@GetMapping("/queryMasterTeacherFormatSelect2")
	@RequiresPermissions(
		value = {"module-student:classes:add","module-student:classes:edit"},
		logical = Logical.OR
	)
	public AjaxResult queryMasterTeacherFormatSelect2() {
		List<Select2> result = moduleUserService.selectList();
		return toSelect2Ajax(result);
	}
}
